package com.aistar.controller;

import com.aistar.pojo.Comment;
import com.aistar.pojo.CommentImages;
import com.aistar.pojo.Customer;
import com.aistar.service.CommentImagesService;
import com.aistar.service.CommentService;
import com.aistar.service.CustomerService;
import com.aistar.service.ProductService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;


import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private ProductService productService;

    @Autowired
    private CommentImagesService commentImagesService;

    //请求得到商品的评论个数
    @GetMapping("/getCount/{productId}")
    @ResponseBody
    public ServerResponse getCount(@PathVariable("productId")Integer productId){
        return commentService.commentCount(productId);
    }

    @PostMapping("/write")
    @ResponseBody
    public ServerResponse toAddComment(Integer customerId,Integer productId, String content, Double commentsStar, HttpServletRequest request){
        System.out.println("进入到添加评论内容的controler层");
        Comment comment = new Comment();
       // Customer customer = (Customer) request.getSession().getAttribute("loginCustomer");
        //利用时间获取一个int类型的值充当评论的id
        System.out.println("CustomerId："+customerId);
        Date date = new Date();
        long time = date.getTime();
        /*int commentId =(int) time%(1000000000);*/
        Integer commentId = (int)time%(1000000000) ;
        comment.setProductId(productId);
        comment.setCommentTime(new Date());
        comment.setCustomerId(customerId);
//        comment.setCustomerId(1);
        comment.setCommentId(commentId);
        comment.setCommentContent(content);
        comment.setCommentsStars(commentsStar);
        comment.setCommentStatus(1);
        return commentService.save(comment);
    }


    //图片上传
    @RequestMapping(value = "/upload/img", method = {RequestMethod.POST})
    @ResponseBody
    public ServerResponse upload(@RequestParam(value="file",required=false) MultipartFile[] files, HttpServletRequest request){
       // Integer commentId = Integer.valueOf(request.getParameter("commentId"));
        Integer commentId = 1;
        System.out.println("传过来的评论id是："+commentId);
        List<String> stringList = new ArrayList<>();
        if (files!=null && files.length>0){
            for (MultipartFile file : files) {
                String originalFilename = file.getOriginalFilename();
                String imgName = UUID.randomUUID().toString().replace("-","");
                String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
                String filepath = request.getSession().getServletContext().getRealPath("/upload");

                File imgFile = new File(filepath);
                if (!imgFile.exists())
                    imgFile.mkdirs();
                String fileName = imgName+extName;

                //将图片设置对应的评论id 添加进mongodb
                CommentImages commentImages = new CommentImages();
                commentImages.setCommentId(commentId);
                commentImages.setCommentImagesStatus(1);
                commentImages.setCommentImagesPath(fileName);
                commentImagesService.save(commentImages);

                String path = filepath+"/"+fileName;
                stringList.add(path);
                try {
                    file.transferTo(new File(imgFile,fileName));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        if (stringList.size()>0)
            return ServerResponse.getDataFailed(stringList);
        else
            return ServerResponse.getDataFailed("上传失败");


       }


    //获取所有评论
    @GetMapping("/getAll/{productId}")
    @ResponseBody
    public ServerResponse getAllComments(@PathVariable("productId")Integer productId){
        Integer page =1;
        return commentService.getAllCommentByProductId(productId,page);

    }


}
